<template>
  <div>
    <div class="search_box_con">
      <div class="search_head">
        <div class="ipt">
          <span class="iconfont icon-fangdajing"></span>
          <input
            type="text"
            placeholder="安踏卫衣"
            v-model="test"
            @input="goSearch"
            @keydown.enter="goTo"
          />
        </div>
        <div class="gohome">
          <p @click="$router.push('/shopping')">取消</p>
        </div>
      </div>
      <div class="history" v-show="!lists">
        <p class="head">历史搜索</p>
        <div class="box">
          <p v-for="(item,index) in historylists" :key=index>{{item}}</p>
        </div>
      </div>
      <div class="recommend" v-show="lists && !dataList">
        <p @click="$router.push('/shopping/details?id=' + lists.goods_id)">
          {{ lists.goods_test }}
        </p>
      </div>
      <div class="goTo_box" v-show="dataList">
        <div
          class="commodity"
          v-for="(item, index) in dataList"
          :key="index"
          @click="$router.push('/shopping/details?id=' + item.goods_id)"
        >
          <div class="commodity_img">
            <img :src="item.goods_img" />
          </div>
          <div class="commodity_text">
            <p class="p">{{ item.goods_test }}</p>
            <div class="price">
              <span class="price_left">￥{{ item.goods_price }}</span
              ><span class="price_right">{{ item.goods_price_num }}人付款</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      test: "", //搜索框内容
      lists: "", //推荐搜索
      dataList: "", //回车搜索
      historylists:[]//搜索历史
    };
  },
  created(){
    //获取vuex数据
    console.log(this.$store.state.history);
    this.historylists = this.$store.state.history;
  },
  methods: {
    goSearch() {
      if (this.test) {
        this.$http.get("/static/search.json").then((data) => {
          let test = data.data;
          let index = test.findIndex((item) => {
            return item.name == this.test;
          });
          if (index == -1) {
            this.lists = "";
          } else {
            this.lists = test[index];
          }
        });
      } else {
        this.lists = "";
        this.dataList = ""
      }
    },
    //回车进入
    goTo() {
      if (this.test) {
        this.$http.get("/static/search.json").then((data) => {
          let test = data.data;
          let index = test.findIndex((item) => {
            return item.name == this.test;
          });
          if (index == -1) {
            this.dataList = "";
          } else {
            let arr = [];
            arr.push(test[index]);
            this.dataList = arr;
          }
        });
        this.$store.commit("addhistory",this.test)
      } else {
        this.dataList = "";
      }
    },
  },
};
</script>

<style lang="less">
.search_box_con {
  height: 100vh;
  background-color: #fff;
  .search_head {
    padding: 10px 20px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .ipt {
      position: relative;
      width: 80%;
      span {
        position: absolute;
        left: 6px;
        top: 6px;
      }
      input {
        width: 100%;
        font-size: 12px;
        border: 0;
        height: 30px;
        padding-left: 25px;
        background-color: #f1f3f4;
      }
    }
    .gohome {
      font-size: 14px;
      font-weight: bold;
    }
  }
  .history {
    padding: 10px 20px;
    .head {
      font-size: 12px;
    }
    .box {
      padding-top: 10px;
      p {
        margin-right: 5px;
        margin-bottom: 5px;
        display: inline-block;
        font-size: 12px;
        padding: 3px 5px;
        background-color: #f1f3f4;
        border-radius: 5px;
      }
    }
  }
  .recommend {
    padding: 10px 20px;
    font-size: 12px;
    p {
      width: 95%;
      padding-bottom: 5px;
      border-bottom: 1px solid #f4f4f4;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
  }
  .goTo_box {
      box-sizing: border-box;
      padding: 0 20px;
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    .commodity {
      width: 50%;
      background-color: #fff;
      .commodity_img {
        display: flex;
        justify-content: center;
        align-content: center;
        img {
          width: 100%;
        }
      }
      .commodity_text {
        .p {
          padding: 0px 15px;
          font-size: 12px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .price {
          display: flex;
          justify-content: space-between;
          padding: 10px 15px 15px;
          font-size: 12px;
          .price_left {
            font-weight: bold;
          }
          .price_right {
            transform: scale(0.9);
            color: #b0b0b2;
          }
        }
      }
    }
  }
}
</style>